package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.config.BusinessException;
import com.emergency.data.entity.RescueTeam;
import com.emergency.data.entity.RescueTeamSupplies;
import com.emergency.data.vo.RescueTeamCountVO;
import com.emergency.data.vo.RescueTeamDetailsVO;
import com.emergency.data.vo.RescueTeamSuppliesVO;
import com.emergency.data.vo.RescueTeamVO;
import com.emergency.http.DefaultResponseCode;
import com.emergency.mapper.RescueTeamMapper;
import com.emergency.mapper.RescueTeamSuppliesMapper;
import com.emergency.mapper.TeamTypeMapper;
import com.emergency.service.RescueTeamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (RescueTeam)救援队伍服务实现类
 *
 * @author makejava
 * @since 2020-09-21
 */
@Service
public class RescueTeamServiceImpl extends ServiceImpl<RescueTeamMapper, RescueTeam> implements RescueTeamService {

    @Resource
    private RescueTeamMapper rescueTeamMapper;

    @Resource
    private RescueTeamSuppliesMapper rescueTeamSuppliesMapper;

    @Resource
    private TeamTypeMapper teamTypeMapper;

    @Override
    public List<RescueTeamVO> getRescueTeamList(Long streetId, Long id, String teamName) {
        //查询条件
        QueryWrapper<RescueTeam> query = new QueryWrapper<>();
        if (streetId != null) {
            query.eq("street_id", streetId);
        }
        if (id != null) {
            query.eq("team_type_id", id);
        }
        if (StrUtil.isNotEmpty(teamName)) {
            query.like("team_name", teamName);
        }
        return rescueTeamMapper.selectList(query).stream().map(c -> {
            RescueTeamVO vo = new RescueTeamVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<RescueTeamCountVO> rescueTeamStatistics() {
//        return rescueTeamMapper.selectList(null).stream().map(c -> {
//            RescueTeamCountVO vo = new RescueTeamCountVO();
//            vo.setName(c.getTeamName());
//            vo.setValue(c.getNumber());
//            return vo;
//        }).collect(Collectors.toList());

        return teamTypeMapper.selectList(null).stream().map(c -> {
            RescueTeamCountVO vo = new RescueTeamCountVO();
            vo.setName(c.getTeamTypeName());
            //查询对应类型下所有队伍
            List<RescueTeam> teamList = rescueTeamMapper.selectList(new QueryWrapper<RescueTeam>()
                    .eq("team_type_id", c.getId()));
            //人数
            int number = 0;
            for (RescueTeam rescueTeam : teamList) {
                number += rescueTeam.getNumber();
            }
            vo.setValue(number);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public RescueTeamDetailsVO getRescueTeamDetail(Long id) {
        RescueTeam rescueTeam = rescueTeamMapper.selectById(id);
        if (rescueTeam == null) {
            throw new BusinessException(DefaultResponseCode.RESCUE_TEAM_NOT_EXIST);
        }
        //返回对象VO
        RescueTeamDetailsVO detailsVO = new RescueTeamDetailsVO();
        BeanUtil.copyProperties(rescueTeam, detailsVO);
        //查询物资列表
        List<RescueTeamSuppliesVO> suppliesList = rescueTeamSuppliesMapper.selectList(new QueryWrapper<RescueTeamSupplies>()
                .eq("rescue_team_id", id)).stream().map(c -> {
            RescueTeamSuppliesVO vo = new RescueTeamSuppliesVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
        detailsVO.setSuppliesList(suppliesList);
        return detailsVO;
    }
}
